package com.david.flowable.controller;

import com.david.common.utils.PageResult;
import com.david.flowable.pojo.dto.FlowTaskDTO;
import com.david.flowable.pojo.vo.FlowQueryVO;
import com.david.flowable.pojo.vo.FlowTaskVO;
import com.david.flowable.service.FlowTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.david.common.utils.ResultUtils.success;

@RestController
@RequestMapping("/flowable/task")
@Slf4j
public class FlowTaskController {
    @Autowired
    private FlowTaskService flowTaskService;

    /**
    * @Description: 完成任务
    * @Param: [flowTaskVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/8
    */
    @PostMapping("/complete")
    public ResponseEntity<Object> taskComplete(@RequestBody FlowTaskVO flowTaskVO) {
        log.info("{}完成任务", flowTaskVO);
        return flowTaskService.complete(flowTaskVO);
    }
    /**
    * @Description: 获取待办列表
    * @Param: [flowQueryVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/8
    */
    @PostMapping("/todolist")
    public ResponseEntity<Object> todoList(@RequestBody FlowQueryVO flowQueryVO){
        log.info("待办列表:{}",flowQueryVO);
        PageResult flowtaskList = flowTaskService.todolist(flowQueryVO);
        return success(true, flowtaskList);
    }

    /**
    * @Description: 获取已办理列表
    * @Param: [flowQueryVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/14
    */
    @PostMapping("/finishedlist")
    public ResponseEntity<Object> finishedlist(@RequestBody FlowQueryVO flowQueryVO){
        log.info("已办列表:{}",flowQueryVO);
        PageResult flowtaskList = flowTaskService.finishedlist(flowQueryVO);
        return success(true, flowtaskList);
    }

    /**
    * @Description: 驳回流程
    * @Param: [flowTaskVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/8
    */
    @PostMapping("/reject")
    public ResponseEntity<Object> reject(@RequestBody FlowTaskVO flowTaskVO) {
        log.info("{}任务驳回", flowTaskVO);
        flowTaskService.reject(flowTaskVO);
        return success(true, "已驳回");
    }

    /**
    * @Description: 查询流程
    * @Param: [procInsId]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/10
    */
    @GetMapping("/records/{procInsId}")
    public ResponseEntity<Object> records(@PathVariable("procInsId") String procInsId) {
        log.info("{}流程记录", procInsId);
        List<FlowTaskDTO> records = flowTaskService.records(procInsId);
        return success(true, records);
    }

    /**
    * @Description: 撤销流程
    * @Param: [flowTaskVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/14
    */
    @PutMapping("/revoke")
    public ResponseEntity<Object> revoke(@RequestBody FlowTaskVO flowTaskVO) {
        log.info("撤销任务{}", flowTaskVO);
        flowTaskService.revoke(flowTaskVO);
        return success(true, "已撤销");
    }

    /**
    * @Description: 获取我的流程
    * @Param: [flowQueryVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/16
    */
    @PostMapping("/myprocess")
    public ResponseEntity<Object> myProcess(@RequestBody FlowQueryVO flowQueryVO) {
        log.info("获取我的流程（我发起的流程）");
        return success(true, flowTaskService.getMyProcess(flowQueryVO));
    }

    /**
    * @Description: 终止流程
    * @Param: [procInsId]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/16
    */
    @PutMapping("/stop/{procInsId}")
    public ResponseEntity<Object> stopProcIns(@PathVariable String procInsId) {
        log.info("终止流程{}", procInsId);
        flowTaskService.stopProcIns(procInsId);
        return success(true, "已取消");
    }
    /**
    * @Description: 删除流程
    * @Param: [procInsId]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/16
    */
    @PutMapping("/remove/{procInsId}")
    public ResponseEntity<Object> removeProcIns(@PathVariable String procInsId) {
        log.info("删除流程{}", procInsId);
        flowTaskService.removeProcIns(procInsId);
        return success(true, "已删除");
    }

    /**
    * @Description: 获取候选任务列表
    * @Param: [flowQueryVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/8/16
    */
    @PostMapping("/candidate")
    public ResponseEntity<Object> getCandidateList(@RequestBody FlowQueryVO flowQueryVO) {
        log.info("获取候选流程");
        PageResult result = flowTaskService.getCandidateList(flowQueryVO);
        return success(true, result);
    }

    @PutMapping("/claim/{taskId}")
    public ResponseEntity<Object> claimTask(@PathVariable String taskId){
        log.info("认领任务{}", taskId);
        String result = flowTaskService.claimTask(taskId);
        return success(true, result);
    }

    @PutMapping("/unclaim/{taskId}")
    public ResponseEntity<Object> unclaimTask(@PathVariable String taskId){
        log.info("认领任务{}", taskId);
        String result = flowTaskService.unclaimTask(taskId);
        return success(true, result);
    }
}
